<?php
namespace app\admin\controller;
use app\common\controller\AdminBase;
use think\DB;
/*内门禁日志管理*/
class SlidingLog extends AdminBase{
	protected $beforeActionList = [
        'before_index'=>['only'=>'index'],
    ];
	function _initialize() {
		if(!in_array(request()->action(),array("export_excel"))){
			parent::_initialize();	
		}
    }
	function index(){
		$this->assign("now",date("Y-m-d"));
		$this->assign("doors",db("SlidingDoor")->select());
		echo $this->fetch();
	}	
	
	/*卡列表数据*/
	function ajax_index(){
		$this->ajax($this->_ajax_index());
	}
	///////////////////////////////////////////////////////////////////////////////////
	/** 
	 * 权限菜单
	 **/
	function before_index(){
		$auth_group=sp_get_auth_menu_3("10041009");
		$this->assign("menus",$auth_group);
	}
	/**
	 * 导出excle
	 * */
	public function export_excel(){
		$titles_des=array("卡号","姓名","类型","刷卡时间","内门禁","状态","错误代码");
		$titles_field=array("card_no","name","owner_type","create_time","door_name","behavior_status","error_msg");
		$data=$this->_ajax_index();
		$list=$data['pageData'];
		foreach ($list as $key => $value) {
			if($value['behavior_status']==1){
				$list[$key]['behavior_status']="成功";
			}else{
				$list[$key]['behavior_status']="失败";
			}
		if($value['owner_type']==1){
				$list[$key]['owner_type']="会员卡";
			}else if($value['owner_type']==2){
				$list[$key]['owner_type']="员工卡";
			}else if($value['owner_type']==3){
				$list[$key]['owner_type']="临时卡";
			}
		}
		$title="内门禁进出场日志";
		echo export_excel($titles_des,$titles_field,$list,$title);
		
	}
	/*获取卡数据*/
	private function _ajax_index(){
		$query=array(
				'behavior_status'=>array("field" =>"behavior_status","operator" =>"="),
				'card_no'=>array('field'=>'card_no',"operator"=>"like"),
				'door_code'=>array('field'=>'door_code',"operator"=>"="),
				);
		$draw=input("draw",0)+1;//请求时间		 
		$where_and=join(" and ",sp_get_param_sql(request()->isPost(),$query));
		
		if(input('behavior_status')==1){
			$where_and.=" and behavior_type in(1)";
		}else{
			$where_and.=" and behavior_type in(1,2)";
		}
		
		if(empty(input('start'))){
			$start=date('Y-m-d')." 00:00:00";
		}else{
			$start=input('start')." 00:00:00";
		}
		if(empty(input('end'))){
			$end=date('Y-m-d')." 23:59:59";
		}else{
			$end=input('end')." 23:59:59";
		}
		$where_and.=" and create_time>'$start' and create_time<'$end'";
		
		$total=db("SlidingLog")->where($where_and)->fetchsql(FALSE)->count(1);
		$list=db("SlidingLog")->where($where_and)->order($this->get_order())->limit($this->get_limit())->fetchsql(FALSE)->select();
		//统计刷卡次数
		$crash_1=db("SlidingLog")->where($where_and)->count(1);
		//统计刷卡人数
		$crash_2=db("SlidingLog")->where($where_and)->fetchsql(FALSE)->count("distinct(card_no)");
		return array('pageData'=>$list,'total'=>$total,"draw"=>$draw,"crash_1"=>$crash_1,"crash_2"=>$crash_2);
	}
}
